package com.wfy.modules.sys.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wfy.common.entity.Result;
import com.wfy.common.entity.ResultGenerate;
import com.wfy.common.security.shiro.utils.ShiroHolder;
import com.wfy.common.web.BaseController;
import com.wfy.modules.sys.dto.UserDto;
import com.wfy.modules.sys.entity.User;
import com.wfy.modules.sys.secutiry.PrincipalUser;
import com.wfy.modules.sys.service.IUserService;
import com.wfy.modules.sys.vo.UserVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/2/23-20:02
 */
@Api("用户管理")
@Controller
@RequestMapping(value = "/v1/sys/users")
public class UserController extends BaseController {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================

    @Resource(name = "userServiceImpl")
    private IUserService<User> userService;

    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    // ================================================================
    // Public or Protected Methods
    // ================================================================

    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Result list(@RequestBody UserDto userDto) {
        int size = userDto.getSize();
        int current = userDto.getCurrent();
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        Page page = new Page(current, size);
        Page<User> users = userService.selectPage(page, new EntityWrapper<>(user));

        List<User> userList = users.getRecords();
        List<UserVo> result = new ArrayList<>(userList.size());
        for (User u : userList) {
            UserVo vo = new UserVo();
            BeanUtils.copyProperties(vo, u);
            result.add(vo);
        }
        return ResultGenerate.getSuccessResult(result);
    }

    /**
     * 根据token获取用户信息
     *
     * @return
     */
    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result userInfo() {
        PrincipalUser user = ShiroHolder.getUser();
        User u = userService.getUserInfo(user.getId());
        UserVo vo = new UserVo();
        vo.build(u);
        return ResultGenerate.getSuccessResult(vo);
    }

    /**
     * 获取用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Result get(@PathVariable(value = "id") String id) {
        User user = userService.getUserInfo(id);
        UserVo vo = new UserVo();
        vo.build(user);
        return ResultGenerate.getSuccessResult(vo);
    }

    /**
     * 创建用户
     *
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Result createUser(@Valid @RequestBody UserDto userDto) {
        User user = userService.createUser(userDto);
        return ResultGenerate.getSuccessResult(user);
    }

    // ================================================================
    // Getter & Setter
    // ================================================================

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
